/**
 * futures : masterlink.futures.dao.GroupDao.java
 */
package masterlink.futures.dao;

import java.util.ArrayList;
import java.util.List;

import masterlink.futures.vo.Account;
import masterlink.futures.vo.Employee;
import masterlink.futures.vo.Equity;
import masterlink.futures.vo.Fuhiva;
import masterlink.futures.vo.Fuhivc;
import masterlink.futures.vo.GID;
import masterlink.futures.vo.GreekConclusion;
import masterlink.futures.vo.GreekVerbS;
import masterlink.futures.vo.Group;
import masterlink.futures.vo.SysGroup;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import baytony.util.Util;

/**
 * TODO Write the description
 *
 * @author : tonyyang
 * Create date : 2011/10/25
 * @version 1.0.0	
 */
@Repository
@Qualifier("GroupDao")
public class GroupDao extends TimesTenHibernateDao<Group,Long> {
	
	private static final String FIND_SYS_GROUP="from Group where system='Y' order by seq";
	private static final String FIND_ADMIN_GROUP="select a.*,b.* from sys_group a, gid b where a.name=b.gid and a.system='Y' order by a.id";
	private static final String FIND_BY_EMP="select b.* from SYS_EMP_GROUP a, SYS_GROUP b where a.GROUP_ID=b.id and a.empno=? order by b.system desc, b.seq, b.id";
	private static final String FIND_EMP_SYS_GROUP="select b.* from SYS_EMP_GROUP a, SYS_GROUP b where a.GROUP_ID=b.id and a.empno=? and b.system='Y' order by b.system desc, b.seq, b.id";
	private static final String FIND_CUST_GROUP="select b.* from SYS_EMP_GROUP a, SYS_GROUP b where a.GROUP_ID=b.id and a.empno=? and b.system='N' order by b.system desc, b.seq, b.id";
	private static final String INSERT_EMP_GROUP="insert into SYS_EMP_GROUP values (?,?,?,?)";
	private static final String DELETE_EMP_GROUP="delete from SYS_EMP_GROUP where empno=? and group_id=?";
	private static final String DELETE_GROUP="delete from SYS_EMP_GROUP where group_id=?";
	
	@SuppressWarnings("unchecked")
	public List<Group> findSysGroups(){
		return (List<Group>) findByHql(FIND_SYS_GROUP);
	}
	
	public List<Group> findByEmp(String empno){
		return findBySql(FIND_BY_EMP, Group.class, empno);
	}
	
	public List<Group> findEmpSysGroup(String empno){
		return findBySql(FIND_EMP_SYS_GROUP, Group.class, empno);
	}
	
	public List<Group> findCustGroup(String empno){
		return findBySql(FIND_CUST_GROUP, Group.class, empno);
	}
	
	public void save(String empno,String groupId,String phone,String send){
		super.executeUpateQuery(INSERT_EMP_GROUP, empno,groupId,phone,send);
	}
	
	public void save(Employee emp,Group group){
		super.executeUpateQuery(INSERT_EMP_GROUP, emp.getEmpno(),group.getId(),null,'N');
	}
	
	public void delete(String empno,Group group){
		super.executeUpateQuery(DELETE_EMP_GROUP, empno,group.getId());
	}
	
	@Override
	public void delete(Group group){
		super.delete(group);
		super.executeUpateQuery(DELETE_GROUP,group.getId());
	}
	
	@SuppressWarnings("unchecked")
	public List<SysGroup> findAdminSysGroup(){
		return create((List<Object[]>)findBySql(FIND_ADMIN_GROUP,new Class[]{Group.class,GID.class},new Object[0]));
	}
	
	private List<SysGroup> create(List<Object[]> all){
		List<SysGroup> res=new ArrayList<SysGroup>();
		if(!Util.isEmptyList(all)){
			for(Object[] o:all){
				Group group=(Group)evict(o[0]);
				GID gid=(GID)evict(o[1]);
				SysGroup e=new SysGroup(group, gid);
				res.add(e);
			}
		}
		log.trace("{} of SysGroup was found.",res.size());
		return res;
	}
}
